use rand::Rng;
use std::{cmp::Ordering, io};

fn main() {
    println!("猜一个数字");
    println!("输入一个数字");

    // 生成一个 1 - 100 的随机数
    let secret_number = rand::thread_rng().gen_range(1, 101);
    // println!("secret_number = {}", secret_number);
    loop {
        let mut guess = String::new();

        // 键盘输入操作
        io::stdin().read_line(&mut guess).expect("输入错误");

        // 处理输入的值 如果是数字则返回数字 否则继续输入
        let guess: u8 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => continue,
        };

        match guess.cmp(&secret_number) {
            Ordering::Less => {
                println!("小了")
            }
            Ordering::Equal => {
                println!("对了");
                break;
            }
            Ordering::Greater => {
                println!("大了")
            }
        }
    }
}
